package com.o.mj.service;

import com.o.mj.dao.WorkPersonMapper;
import com.o.mj.pojo.Register;
import com.o.mj.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Service
public class PersonService {

    @Autowired
    WorkPersonMapper workPersonMapper;

    /**
     * imgFile 图片本地存储路径
     */
    public String getImgFileToBase64(String userId) {
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream inputStream = null;
        byte[] buffer = null;
        //读取图片字节数组
        try {
            String personName = workPersonMapper.personName(userId);
            inputStream = new FileInputStream("C:\\Ming\\" + personName);
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            buffer = new byte[count];
            inputStream.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    // 关闭inputStream流
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 对字节数组Base64编码
        String BaseSrc = new BASE64Encoder().encode(buffer);
        String BaseSrcA = "data:image/png;base64," + BaseSrc;

        return BaseSrcA;
    }

    /**
     * 修改信息
     */
    public void personMessage(UserInfo userInfo) {
         workPersonMapper.personMessage(userInfo);
    }
}
